package ru.papergames.web.blines.battle.network.serverpackets;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.common.network.clientcore.ReceivablePacket;
import ru.papergames.web.blines.battle.network.ClientWebServer;
import ru.papergames.web.blines.battle.network.clientpackets.WebServerClientPacket;

/**
 * @author: atroshanin
 * Date: 18.04.2010
 * Time: 14:42:48
 */
public abstract class WebServerServerPacket extends ReceivablePacket<ClientWebServer> implements WebServerServerPacketTypes {
    private static Log log = LogFactory.getLog(WebServerServerPacket.class);

    @Override
    protected boolean read() {
        try {
            readImpl();
            return true;
        }
        catch (Exception e) {
            //logger.severe("Client: " + getClient().toString() + " - Failed reading: " + getType() + " - L2J Server Version: " + Config.SERVER_VERSION + " - DP Revision: " + Config.DATAPACK_VERSION);
            e.printStackTrace();
        }
        return false;
    }

    protected abstract void readImpl();

    @Override
    public void run() {
        try {
            // flood protection
//            if (GameTimeController.getGameTicks() - getClient().packetsSentStartTick > 10) {
//                getClient().packetsSentStartTick = GameTimeController.getGameTicks();
//                getClient().packetsSentInSec = 0;
//            } else {
//                getClient().packetsSentInSec++;
//                if (getClient().packetsSentInSec > 12) {
//                    if (getClient().packetsSentInSec < 100) {
//                        sendPacket(ActionFailed.STATIC_PACKET);
//                    }
//                    return;
//                }
//            }

            runImpl();

            /* Removes onspawn protection - player has faster computer than average
                *
                * True for these packets:
                * AttackRequest
                * MoveBackwardToLocation
                * RequestActionUse
                * RequestMagicSkillUse
                *
                * it could include pickup and talk too, but less is better
                */
            /*
            if (triggersOnActionRequest() && getClient().getActiveChar() != null)
            	getClient().getActiveChar().onActionRequest();
            */
        }
        catch (Throwable t) {
            //logger.severe("Client: " + getClient().toString() + " - Failed running: " + getType() + " - L2J Server Version: " + Config.SERVER_VERSION + " - DP Revision: " + Config.DATAPACK_VERSION);
            t.printStackTrace();
            log.error(t);
        }
    }

    protected abstract void runImpl();

    protected final void sendPacket(WebServerClientPacket gsp) {
        getClient().sendPacket(gsp);
    }

    /**
     * @return A String with this packet name for debuging purposes
     */
    public abstract String getType();

    /**
     * Overriden with true value on some packets that should disable spawn protection
     */
    protected boolean triggersOnActionRequest() {
        return false;
    }
}
